# -*-coding:utf-8-*-
# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 20:23
# @Author  : gaojy
# @Site    : 
# @File    : conftest.py
# @Software: PyCharm
import logging

import pytest
from common.common_function import get_parent_path, write_sys_code_yaml, read_conf_yaml, write_config_yaml,\
    clear_sys_code_yaml, clear_config_yaml


# 执行前清理extract.yaml
@pytest.fixture(scope = "session", autouse = True)
def clear_extract_yaml():
    print("清理11111")
    with open(get_parent_path() + "/extract.yaml", 'w', encoding = 'utf-8') as f:
        f.truncate()


# # 执行前清理log
@pytest.fixture(scope = "session", autouse = True)
def clear_log():
    with open(get_parent_path() + "/log/log.txt", 'w', encoding = 'utf-8') as f:
        f.truncate()


# 钩子hook函数
# 注册环境变量
def pytest_addoption(parser):
    parser.addoption("--env", action = "store",
                     default = "test",
                     help = "环境变量切换，将--env添加到pytest中")
    parser.addoption("--sysCode", action = "store",
                     help = "系统切换")


#  钩子函数
def pytest_configure(config):
    print(f"当前测试环境是{config.getoption('--env')}")
    print(f"当前测试系统是{config.getoption('--sysCode')}")
    clear_sys_code_yaml()
    # 将测试系统代码写入syscode.yaml
    write_sys_code_yaml(config.getoption("--sysCode"))
    # 获取测试环境的值
    one_node = config.getoption("--env")
    two_node = config.getoption("--sysCode")
    clear_config_yaml()
    # 将获取到的值写入config.yaml中
    write_config_yaml(read_conf_yaml(one_node, two_node))
    # 获取测试环境的值
    one_node = config.getoption("--env")
    two_node = config.getoption("--sysCode")
    clear_config_yaml()
    # 将获取到的值写入config.yaml中
    write_config_yaml(read_conf_yaml(one_node, two_node))
